/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connectionmanager;

/**
 *
 * @author Clarence
 */
import java.sql.*;
import java.util.*;
import java.io.*;

public class DB {

    private String hostname;
    private String port;
    private String databaseName;
    private String user;
    private String password;
    private String sql = "";
    private Connection conn = null;
    private Statement stmt = null;
    private ResultSet rs = null;

    private static String JDBC_DRIVER = "jdbc.driver";
    private static String JDBC_URL = "jdbc.url";
    private static String JDBC_USER = "jdbc.user";
    private static String JDBC_PASSWORD = "jdbc.password";
    private static Properties props = new Properties();

    static {
        try {
            // a way to retrieve the data in
            // connection.properties found
            // in WEB-INF/classes
            InputStream is = DB.class.getResourceAsStream("./connection.properties");
            props.load(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class.forName(props.getProperty(JDBC_DRIVER)).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DB() {
        try {
            /*

            Will be implemented later

            InputStream is = DB.class.getResourceAsStream("./connection.properties");
            props.load(is);

            hostname = props.getProperty("db.hostname");
            port = props.getProperty("db.port");
            databaseName = props.getProperty("db.databaseName");
            user = props.getProperty("db.user");
            password = props.getProperty("db.password");

             */
            
            hostname = "localhost";
            port = "3306";
            databaseName = "mictycoon";
            user = "mictycoon";
            password = "mictycoon1";
            

        } catch (Exception ex) {
            System.out.println("DB() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public void connect() {
        try {
            Class.forName(props.getProperty(JDBC_DRIVER)).newInstance();
            //String dbUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + databaseName + "?user=" + user + "&password=" + password + "";
            conn = DriverManager.getConnection(props.getProperty(JDBC_URL),
                props.getProperty(JDBC_USER),
                props.getProperty(JDBC_PASSWORD));
            stmt = conn.createStatement();
        } catch (Exception ex) {
            System.out.println("DB.connect() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public void query(String sqlStatement) {
        //System.out.print(sqlStatement);
        try {
            rs = stmt.executeQuery(sqlStatement);
        } catch (Exception ex) {
            System.out.println(sqlStatement);
            System.out.println("DB.query() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public boolean execute(String sqlStatement) {
        //System.out.print(sqlStatement);
        try {
            return stmt.execute(sqlStatement);
        } catch (Exception ex) {
            System.out.println(sqlStatement);
            System.out.println("DB.execute() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * Move the result set reader to the next result.
     * Returns true when there is still next row of value.
     */
    public boolean next() {


        try {
            if (rs != null) {
                return rs.next();
            }
        } catch (Exception ex) {
            System.out.println("DB.next() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * Returns the string data under the stated column name
     */
    public String getString(String attribute) {
        try {
            if (rs != null) {
                return rs.getString(attribute);
            } else {
                throw new NullPointerException();
            }
        } catch (Exception ex) {
            System.out.println("DB.getString() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        throw new NullPointerException();
    }

    /**
     * Returns the int data under the stated column name
     */
    public Integer getInt(String attribute) {
        try {
            if (rs != null) {
                return rs.getInt(attribute);
            } else {
                throw new NullPointerException();
            }
        } catch (Exception ex) {
            System.out.println("DB.getInt() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        throw new NullPointerException();
    }

    /**
     * Returns the double data under the stated column name
     */
    public Double getDouble(String attribute) {
        try {
            if (rs != null) {
                return rs.getDouble(attribute);
            } else {
                throw new NullPointerException();
            }
        } catch (Exception ex) {
            System.out.println("DB.getDouble() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        throw new NullPointerException();
    }

    /**
     * Returns the long data under the stated column name
     */
    public Long getLong(String attribute) {
        try {
            if (rs != null) {
                return rs.getLong(attribute);
            } else {
                throw new NullPointerException();
            }
        } catch (Exception ex) {
            System.out.println("DB.getLong() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        throw new NullPointerException();
    }

    /**
     * Returns the Timestamp under the stated column name
     */
    public java.sql.Timestamp getTimestamp(String attribute) {
        try {
            if (rs != null) {
                return rs.getTimestamp(attribute);
            } else {
                throw new NullPointerException();
            }
        } catch (Exception ex) {
            System.out.println("DB.getTimestamp() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        throw new NullPointerException();
    }

    public int getLastInsertedID() {
        try {
            rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
            rs.next();
            int lastInsertedID = rs.getInt(1);
            return lastInsertedID;
        } catch (Exception ex) {
            System.out.println("DB.getLastInsertedID() ran into an error " + ex.getMessage());
            ex.printStackTrace();
        }
        return 0;
    }

    public void close() {
        try {
            if (rs != null) {
                rs.close();
            }

            if (stmt != null) {
                stmt.close();
            }

            if (conn != null) {
                conn.close();
            }
        } catch (Exception ex) {
            System.out.println("DB.close() ran into an error " + ex.getMessage());
            ex.printStackTrace();

        }
    }
}
